package com.jing.yao.modules.resource.controller;

import com.jing.yao.bean.KeyValue;
import com.jing.yao.bean.ResponseBean;
import com.jing.yao.bean.TreeNode;
import com.jing.yao.component.controller.YaoBaseController;
import com.jing.yao.component.service.YaoBaseService;
import com.jing.yao.modules.resource.controller.dto.ResourceDTO;
import com.jing.yao.modules.resource.controller.param.ResourceParam;
import com.jing.yao.modules.resource.dao.entity.ResourceEntity;
import com.jing.yao.modules.resource.service.ResourceService;
import com.jing.yao.modules.role.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 菜单controller
 *
 * @author lirui
 * @since 2020-06-13
 */
@RestController
@RequestMapping("/resource")
public class ResourceController extends YaoBaseController<ResourceParam, ResourceEntity, ResourceDTO> {

    @Autowired
    private ResourceService resourceService;

    @Autowired
    private RoleService roleService;

    @Override
    public YaoBaseService<ResourceParam, ResourceEntity> getService() {
        return resourceService;
    }

    @Override
    public ResourceEntity getEntity() {
        return new ResourceEntity();
    }

    @Override
    public ResourceDTO getDTO() {
        return new ResourceDTO();
    }

    @RequestMapping(value = "/listResources/{flag}", method = RequestMethod.GET)
    public ResponseBean listResources(@PathVariable("flag") Long flag) {
        List<KeyValue> keyValues;
        if (flag == 1) {
            keyValues = resourceService.listParents();
        } else {
            keyValues = resourceService.listMenus();
        }
        return successBean(keyValues);
    }

    /**
     * 角色分配资源
     *
     * @return
     */
    @RequestMapping(value = "/getTree/{roleCode}", method = RequestMethod.GET)
    public ResponseBean getTree(@PathVariable("roleCode") String roleCode) {

        //获取所有资源列表
        List<TreeNode> tree = resourceService.getTree();
        //获取当前角色已拥有的资源
        List<String> roleTree = resourceService.getRoleTree(roleCode);
        //设置选中项
//		setChecked(tree,roleTree);
        Map<String, Object> map = new HashMap<>(32);
        map.put("tree", tree);
        map.put("roleTree", roleTree);
        return successBean(map);
    }
}
